/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.information.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.information.domain.Slide;
import me.zhengjie.modules.information.service.SlideService;
import me.zhengjie.modules.information.domain.vo.SlideQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author yiwei.he
* @date 2024-05-08
**/
@RestController
@RequiredArgsConstructor
@Slf4j
@Api(tags = "轮播图模块管理")
@RequestMapping("/api/slide")
public class SlideController {

    private final SlideService slideService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportSlide(HttpServletResponse response, SlideQueryCriteria criteria) throws IOException {
        log.info("/api/slide/download  轮播图模块导出数据：{}",criteria);
        slideService.download(slideService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询轮播图模块")
    @ApiOperation("查询轮播图模块")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<PageResult<Slide>> querySlide(SlideQueryCriteria criteria, Page<Object> page){
        log.info("/api/slide  查询轮播图模块：{},page:{}",criteria,page);
        return new ResponseEntity<>(slideService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增轮播图模块")
    @ApiOperation("新增轮播图模块")
    public ResponseEntity<Object> createSlide(@Validated @RequestBody Slide resources){
        log.info("/api/slide  新增轮播图模块：{}",resources);
        slideService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改轮播图模块")
    @ApiOperation("修改轮播图模块")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> updateSlide(@Validated @RequestBody Slide resources){
        log.info("/api/slide  修改轮播图模块：{}",resources);
        slideService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除轮播图模块")
    @ApiOperation("删除轮播图模块")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteSlide(@RequestBody List<Long> ids) {
        log.info("/api/slide  修改轮播图模块：{}",ids);
        slideService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
